import React, { useState } from 'react';
import { HashRouter as Router, Route, Switch } from 'react-router-dom';
import Home from './Bookkeeping/Home';
import Edit from './Bookkeeping/Edit';
import { eventList, categories } from './TestData';
import { PublicContent, FlatternArr, ID } from 'react-fule-ui';
import './index.scss';

export default function Pages() {
  const DatDefault = {
    items: FlatternArr(eventList),
    categories: FlatternArr(categories),
  };
  // 合并数据
  const [withData, setWithData] = useState(DatDefault);
  const actions = {
    deletItem: (item) => {
      delete withData.items[item.id];
      setWithData(withData);
    },
    createItem: (data, categoryId) => {
      const newId = ID();
      data.timestamp = new Date(data.time).getTime(); // 排序用的
      const newItem = { ...data, id: newId, cid: categoryId };
      const newItemS = { ...withData.items, [newId]: newItem };
      const newData = { items: newItemS, categories: withData.categories };
      setWithData(newData);
    },
  };
  return (
    <PublicContent.Provider
      value={{
        withData,
        actions,
      }}
    >
      <Router>
        <Switch>
          <Route exact path="/Pages/Bookkeeping" component={Home} />
          <Route path="/Pages/Bookkeeping/Edit" component={Edit} />
        </Switch>
      </Router>
    </PublicContent.Provider>
  );
}
